package com.example.controler;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

import com.example.callback.onSmsReceiverListenner;

public class SmsReceiver extends BroadcastReceiver {

	private onSmsReceiverListenner listener;

	public SmsReceiver(onSmsReceiverListenner listener) {
		this.listener = listener;
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		Bundle extract = intent.getExtras();
		String message = "";
		if (extract != null) {
			Object[] smsExtract = (Object[]) extract.get("pdus");

			for (int i = 0; i < smsExtract.length; i++) {

				byte[] pdu = (byte[]) smsExtract[i];
				SmsMessage sms = SmsMessage.createFromPdu(pdu);
				message = sms.getDisplayMessageBody();
				String address = sms.getDisplayOriginatingAddress();
				// Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
				listener.onSmsReceiver(address, message);
				abortBroadcast();
			}
		}

	}
}
